package com.ruoyi.system.service.project.impl;

import java.util.List;

import com.ruoyi.system.domain.project.standard.Standard;
import com.ruoyi.system.domain.project.standard.StandardTeacher;
import com.ruoyi.system.mapper.project.StandardMapper;
import com.ruoyi.system.mapper.project.StandardTeacherMapper;
import com.ruoyi.system.service.project.IStandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 标准Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-28
 */
@Service
public class StandardServiceImpl implements IStandardService
{
    @Autowired
    private StandardMapper standardMapper;
    @Autowired
    private StandardTeacherMapper standardTeacherMapper;

    /**
     * 查询标准
     * 
     * @param id 标准主键
     * @return 标准
     */
    @Override
    public Standard selectStandardById(Long id)
    {
        Standard standard  =standardMapper.selectStandardById(id);
        StandardTeacher standardTeacher = new StandardTeacher();
        standardTeacher.setStandardId(id);
        List<StandardTeacher> list = standardTeacherMapper.selectStandardTeacherList(standardTeacher);
        standard.setTeachers(list);
        return standard;
    }

    /**
     * 查询标准列表
     * 
     * @param standard 标准
     * @return 标准
     */
    @Override
    public List<Standard> selectStandardList(Standard standard)
    {
        List<Standard> list = standardMapper.selectStandardList(standard);
        for (Standard standard1 : list){
            StandardTeacher standardTeacher = new StandardTeacher();
            standardTeacher.setStandardId(standard1.getId());
            List<StandardTeacher> list1 = standardTeacherMapper.selectStandardTeacherList(standardTeacher);
            standard1.setTeachers(list1);
        }
        return list;
    }

    /**
     * 新增标准
     * 
     * @param standard 标准
     * @return 结果
     */
    @Override
    public int insertStandard(Standard standard)
    {
        if(standard.getId()!=null){
            StandardTeacher standardTeacher = new StandardTeacher();
            standardTeacher.setStandardId(standard.getId());
            standardTeacher.setUserId(standard.getUserId());
            standardTeacher.setUserName(standard.getUserName());
            standardTeacher.setUserType(standard.getUserType());
            standardTeacherMapper.insertStandardTeacher(standardTeacher);
        }else {
            int result = standardMapper.insertStandard(standard);
            if(result>0){
                StandardTeacher standardTeacher = new StandardTeacher();
                standardTeacher.setStandardId(standard.getId());
                standardTeacher.setUserId(standard.getUserId());
                standardTeacher.setUserName(standard.getUserName());
                standardTeacher.setUserType(standard.getUserType());
                standardTeacherMapper.insertStandardTeacher(standardTeacher);
            }
        }
        return 1;
    }

    /**
     * 修改标准
     * 
     * @param standard 标准
     * @return 结果
     */
    @Override
    public int updateStandard(Standard standard)
    {
        return standardMapper.updateStandard(standard);
    }

    /**
     * 批量删除标准
     * 
     * @param ids 需要删除的标准主键
     * @return 结果
     */
    @Override
    public int deleteStandardByIds(Long[] ids)
    {
        for (Long id : ids){
            StandardTeacher standardTeacher = new StandardTeacher();
            standardTeacher.setStandardId(id);
            List<StandardTeacher> list = standardTeacherMapper.selectStandardTeacherList(standardTeacher);
            if(list!=null && !list.isEmpty()){
                for (StandardTeacher standardTeacher1 : list){
                    standardTeacherMapper.deleteStandardTeacherById(standardTeacher1.getId());
                }
            }
        }
        return standardMapper.deleteStandardByIds(ids);
    }

    /**
     * 删除标准信息
     * 
     * @param id 标准主键
     * @return 结果
     */
    @Override
    public int deleteStandardById(Long id)
    {
        return standardMapper.deleteStandardById(id);
    }
    /**
     * 根据key查询标准信息
     * @param data
     * @return
     */
    @Override
    public Standard getInfoByKey(String data) {
        return standardMapper.getInfoByKey( data);
    }
}
